//
//  KHPhotoBrowserCell.m
//  KissHealth
//
//  Created by Mark on 2017/1/12.
//  Copyright © 2017年 Mark. All rights reserved.
//

#import "KHPhotoBrowserCell.h"
#import "KHProgressView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "KHCustomTool.h"
#import "KHImageTool.h"
@interface KHPhotoBrowserCell ()
@property(nonatomic,weak) UIScrollView *scrollView;

@property(nonatomic,weak) KHProgressView *progressView;

@end
@implementation KHPhotoBrowserCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.contentView.bounds];
    scrollView.width -=20;
    [self.contentView addSubview:scrollView];
    self.scrollView = scrollView;
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.userInteractionEnabled = YES;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.frame = CGRectMake(0, 0, kSCREEN_W, kSCREEN_H);
    [self.contentView addSubview:imageView];
    self.imageView = imageView;
    
    KHProgressView *progressView = [[KHProgressView alloc]init];
    progressView.bounds = CGRectMake(0, 0, 50, 50);
    progressView.center = CGPointMake(kSCREEN_W *0.5, kSCREEN_H *0.5);
    progressView.hidden = YES;
    progressView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:progressView];
    self.progressView = progressView;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewClick)];
    [imageView addGestureRecognizer:tap];
    
    
}
- (void)imageViewClick{
    if ([self.delegate respondsToSelector:@selector(imageViewClick)]) {
        [self.delegate imageViewClick];
    }
}

- (void)setPicURL:(NSString *)picURL{
    _picURL = picURL;
    if ([picURL hasPrefix:@"local://"]) {
        self.progressView.hidden = YES;
        NSString *base64String = [KHCustomTool getValue:picURL];
        UIImage *image = [KHImageTool imageWithBase64String:base64String];
        self.imageView.image = image;
        
    }else{
        self.progressView.hidden = NO;
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:picURL] placeholderImage:[UIImage imageNamed:@""] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            self.progressView.progress = (CGFloat)receivedSize/(CGFloat)expectedSize;
            
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            self.progressView.hidden = YES;
        }];
        

    }
    self.scrollView.contentSize = CGSizeMake(0, kSCREEN_H);
    
}


@end
